package com.rfid.dao.jdbc;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.rfid.dao.interfaces.AlumnoDao;
import com.rfid.domain.Alumno;
import com.rfid.domain.Carrera;

@SuppressWarnings({ "rawtypes", "unchecked" })
@Repository
public class AlumnoJdbc implements AlumnoDao {
	@Autowired
	private JdbcTemplate jdbcTemplate;

	@Override
	public Alumno obtenerPorNombre(String apellidos) throws SQLException {
		String Sql = "SELECT *FROM alumno WHERE apellidos LIKE ? OR nombres LIKE ? ";
		List matches = jdbcTemplate.query(Sql, new Object[] { apellidos },
				new RowMapper() {
					@Override
					public Object mapRow(ResultSet rs, int rowNum)
							throws SQLException {
						Alumno alumno = new Alumno();
						alumno.setIdAlumno(rs.getInt("idAlumno"));
						alumno.setApellidos(rs.getString("apellidos"));
						alumno.setNombres(rs.getString("nombres"));
						alumno.setCodigo(rs.getString("codigo"));
						alumno.getMyCarrera().setIdCarrera(
								rs.getInt("idcarrera"));
						return alumno;
					}
				});
		return (matches.size() > 0 ? (Alumno) matches : null);
		// return null;
	}

	@Override
	public List<Alumno> listarPorNombre(String apellidos) throws SQLException {
		String Sql = "SELECT * FROM alumno WHERE apellidos LIKE ? OR nombres LIKE ?";
		List matches = jdbcTemplate.query(Sql, new Object[] {
				"%" + apellidos + "%", "%" + apellidos + "%" },
				new RowMapper() {
					@Override
					public Object mapRow(ResultSet rs, int rowNum)
							throws SQLException {

						Alumno alumno = new Alumno();
						Carrera carrera = new Carrera();

						alumno.setIdAlumno(rs.getInt("idalumno"));
						alumno.setNombres(rs.getString("nombres"));
						alumno.setApellidos(rs.getString("apellidos"));
						alumno.setCodigo(rs.getString("codigo_alumno"));

						carrera.setIdCarrera(rs.getInt("idcarrera"));
						alumno.setMyCarrera(carrera);

						return alumno;

					}
				});
		return (matches.size() > 0 ? (List<Alumno>) matches : null);

	}

	@Override
	public Alumno obtenerPorCodigo(String codigoAlumno) throws SQLException {
		String SQL = "SELECT * FROM alumno WHERE codigo_alumno = ? LIMIT 1";
		List matches = jdbcTemplate.query(SQL, new Object[] { codigoAlumno },
				new RowMapper() {

					@Override
					public Object mapRow(ResultSet rs, int rowNum)
							throws SQLException {
						Alumno alumno = new Alumno();
						Carrera carrera = new Carrera();

						alumno.setIdAlumno(rs.getInt("idalumno"));
						alumno.setNombres(rs.getString("nombres"));
						alumno.setApellidos(rs.getString("apellidos"));
						alumno.setCodigo(rs.getString("codigo_alumno"));

						carrera.setIdCarrera(rs.getInt("idcarrera"));
						alumno.setMyCarrera(carrera);

						return alumno;
					}

				});
		return (matches.size() > 0 ? (Alumno) matches.get(0): null);
	}
}
